import 'dart:math';

import 'package:flutter/material.dart';

class OpenBoxPage extends StatefulWidget {
  const OpenBoxPage({super.key});

  @override
  State<OpenBoxPage> createState() => _OpenBoxPageState();
}

class _OpenBoxPageState extends State<OpenBoxPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;
  bool _isOpening = false;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 3000), // 总时长从2000ms增加到3000ms
      vsync: this,
    );

    _animation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    );

    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        setState(() => _isOpening = false);
      }
    });
  }

  void _startOpenAnimation() {
    if (!_isOpening) {
      setState(() => _isOpening = true);
      // _controller.repeat();
      _controller.forward();
    }
  }

  void _resetAnimation() {
    _controller.reset();
    setState(() => _isOpening = false);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        color: Colors.black.withOpacity(0.5),
        child: Center(
          child: GestureDetector(
            onTap: _isOpening ? null : _startOpenAnimation,
            onDoubleTap: _resetAnimation,
            child: AnimatedBuilder(
              animation: _animation,
              builder: (context, child) {
                if (_animation.value == 0.0) {
                  // 初始状态显示未开箱图片
                  return RotationTransition(
                    turns: _controller,
                    child: Image.asset(
                      'assets/images/box/1.png',
                      width: 300,
                      height: 300,
                      fit: BoxFit.contain,
                    ),
                  );
                } else if (_animation.value < 0.8) {
                  // 第二阶段：展示开箱中图片(80%)
                  print(_animation.value);
                  return Stack(
                    children: [
                      Opacity(
                        opacity: 1 - (_animation.value / 0.8),
                        child: Image.asset(
                          'assets/images/box/1.png',
                          width: 300,
                          height: 300,
                          fit: BoxFit.contain,
                        ),
                      ),
                      Opacity(
                        opacity: _animation.value / 0.8,
                        child: Image.asset(
                          'assets/images/box/2.png',
                          width: 300,
                          height: 300,
                          fit: BoxFit.contain,
                        ),
                      ),
                    ],
                  );
                } else {
                  // 第三阶段：立体左右旋转展示最终商品图片(20%)
                  final rotationY = (_animation.value - 0.8) / 0.2 * 2 * pi;
                  return Transform(
                    transform: Matrix4.identity()
                      ..setEntry(3, 2, 0.001)
                      ..rotateY(rotationY),
                    alignment: Alignment.center,
                    child: Image.asset(
                      'assets/images/box/3.png',
                      width: 300,
                      height: 300,
                      fit: BoxFit.contain,
                    ),
                  );
                }
              },
            ),
          ),
        ),
      ),
    );
  }
}


void main() {
  Future.any([futureA(), futureB()]).then((res){
    print(res);
  });
}

Future futureA() {
  return Future.delayed(Duration(seconds: 3), () {
    print("futureA");
    return "A";
  });
}

Future futureB() {
  return Future.delayed(Duration(seconds: 2), () {
    print("futureB");
  });
}